package design2.strategy.example.strategies;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class PayByPayPal implements PayStrategy{

	private static final Map<String,String> DATA_BASE=new HashMap<>();


	private final BufferedReader READER=new BufferedReader(new InputStreamReader(System.in));

	private String email;

	private String password;

	private boolean signedIn;

	static {
		DATA_BASE.put("admin","admin@126.com");
		DATA_BASE.put("qing","qing@126.com");
	}

	@Override
	public boolean pay(int paymentAmount) {
		if(signedIn){
			System.out.println("用户支付了"+paymentAmount);
			return  true;
		}
		return false;
	}

	@Override
	public void collectPaymentDetails() {
		try {
			while (!signedIn) {
				System.out.println("输入用户的电子邮件：");
				email=READER.readLine();
				System.out.println("输入用户的密码：");
				password=READER.readLine();

				if(verify()){
					System.out.println("支付成功");
				}else{
					System.out.println("邮箱或密码错误");
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private boolean verify() {
		setSignedIn(email.equals(DATA_BASE.get(password)));
		return  signedIn;
	}

	public void setSignedIn(boolean signedIn) {
		this.signedIn = signedIn;
	}
}
